package com.spzx.cart.controller;

import com.spzx.cart.api.domain.CartInfo;
import com.spzx.cart.service.CartService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//@RequestMapping
@Tag(name = "购物车接口")
@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    @Operation(summary = "添加购物车")
    @GetMapping("/addToCart/{skuId}}/{skuNum}")
    public AjaxResult addToCart(@PathVariable("skuId") Long skuId,@PathVariable("skuNum") Integer skuNum){
        cartService.addToCart(skuId,skuNum);
        return AjaxResult.success();
    }

    @Operation(summary = "获取购物车列表")
    @GetMapping("/cartList")
    public AjaxResult cartList(){
        List<CartInfo> cartInfoList = cartService.cartList();
        return AjaxResult.success(cartInfoList);
    }

    @Operation(summary = "删除购物车中购物项")
    @DeleteMapping("/deleteCart/{skuId}")
    public AjaxResult deleteCart(@PathVariable Long skuId){
        //调用CartService中的根据skuId删除购物项目
        cartService.deleteCartInfo(skuId);
        return AjaxResult.success();
    }

    @Operation(summary = "清空购物车")
    @GetMapping("/clearCart")
    public AjaxResult clearCart(){
        //调用CartService中的根据skuId删除购物项目
        cartService.clearCartInfo();
        return AjaxResult.success();
    }

    @Operation(summary = "更新购物车的选中状态")
    @GetMapping("/checkCart/{skuId}/{status}")
    public AjaxResult checkCartInfo(@PathVariable Long skuId,@PathVariable Integer status){
        cartService.updateCartInfo(skuId,status);
        return AjaxResult.success();
    }

    @Operation(summary = "全选或全不选购物项")
    @GetMapping("/allCheckCart/{status}")
    public AjaxResult allCheckCart(@PathVariable Integer status){
        cartService.allCheckCart(status);
        return AjaxResult.success();
    }

    @InnerAuth
    @Operation(summary = "调用已选中的购物项的接口")
    @GetMapping("/getCheckedCartInfo/{userId}")
    public R<List<CartInfo>> getCheckedCartInfo(@PathVariable Long userId){
        //调用CartService中获取已选中的购物项的方法
        List<CartInfo> cartInfoList = cartService.getCheckedCartInfo(userId);
        return R.ok(cartInfoList);
    }

    @InnerAuth
    @Operation(summary = "调用清除已选中的购物项的接口")
    @GetMapping("/clearCheckedCartInfo/{userId}")
    public R<?> clearCheckedCartInfo(@PathVariable Long userId){
        //调用CartService中获取已选中的购物项的方法
        cartService.clearCheckedCartInfo(userId);
        return R.ok();
    }

}
